package com.my.lab.activity;

import java.io.File;
import java.io.FileOutputStream;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.google.zxing.BarcodeFormat;
import com.my.lab.R;
import com.my.lab.util.ToolsUtil;

public class QrActivity extends Activity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.qt_layout);
        String content = "http://orgcent.com/";
        final int desiredWidth = 120;
        final int desiredHeight = 120;
        final String imageFileName = "orgcent.com.png";
        FileOutputStream fos = null;
        Bitmap bitmap = null;
        try {//生成二维码图像
            bitmap = ToolsUtil.encodeAsBitmap(content, BarcodeFormat.QR_CODE, desiredWidth, desiredHeight);
            if(null != bitmap) {//将二维码图像保存到文件
                File file = new File(Environment.getExternalStorageDirectory(), imageFileName);
                fos = new FileOutputStream(file);
                bitmap.compress(Bitmap.CompressFormat.PNG, 0, fos);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(null != fos) {
                try {
                    fos.close();
                } catch (Exception e) {}
            }
        }

        //显示QRCode
        if(null != bitmap) {
            ImageView iv = new ImageView(this);
            iv.setImageBitmap(bitmap);
            iv.setScaleType(ScaleType.FIT_CENTER);
            setContentView(iv, new LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT, android.view.ViewGroup.LayoutParams.FILL_PARENT
                    ));
        }
    }

}
